import { Provide } from '@midwayjs/core';
import { Repair } from '../entity/repair.entity';
import {
  Pagination,
  RepairAddItem,
  RepairEditItems,
  RepairId,
  RepairsStatusDto,
} from '../model';
import { House } from '../entity/house.entity';
import { RepairItem } from '../entity/repairItem.entity';
import { User } from '../entity/user.entity';
import dayjs = require('dayjs');
// 定义扁平化 DTO
class RepairUseDTO {
  pointName: string;
  building: string;
  room: string;
  repairItemName: string;
  userName: string;
  constructor(repair: any) {
    if (repair.house) {
      this.pointName = repair.house.pointName;
      this.building = repair.house.building;
      this.room = repair.house.room;
    }
    if (repair.repairItem) {
      this.repairItemName = repair.repairItem.name;
    }
    if (repair.user) {
      this.userName = repair.user.name;
    }
    delete repair.house;
    delete repair.repairItem;
    delete repair.user;
    Object.assign(this, repair);
  }
}

@Provide()
export class RepairService {
  async getList(req: Pagination) {
    const { count, rows } = await Repair.findAndCountAll({
      limit: req.pageSize,
      offset: (req.pageNo - 1) * req.pageSize,
      attributes: { exclude: ['createdAt', 'updatedAt'] },
      include: [
        {
          model: House,
          attributes: ['pointName', 'building', 'room'],
        },
        {
          model: RepairItem,
          attributes: ['name'],
        },
        {
          model: User,
          attributes: ['name'],
        },
      ],
      order:[['id','DESC']]
    });
    const list = rows.map(
      repair => new RepairUseDTO(repair.get({ plain: true }))
    );
    return {
      count,
      rows: list,
    };
  }

  async listByPerson(req: Pagination, userId: number) {
    const { count, rows } = await Repair.findAndCountAll({
      where: {
        userId: userId,
      },
      limit: req.pageSize,
      offset: (req.pageNo - 1) * req.pageSize,
      attributes: { exclude: ['createdAt', 'updatedAt'] },
      include: [
        {
          model: House,
          attributes: ['pointName', 'building', 'room'],
        },
        {
          model: RepairItem,
          attributes: ['name'],
        },
        {
          model: User,
          attributes: ['name'],
        },
      ],
      order:[['id','DESC']]
    });
     const list = rows.map(
      repair => new RepairUseDTO(repair.get({ plain: true }))
     );
    list.map(item => {
        item['appointment'] = dayjs(item['appointment']).format('YYYY-MM-DD HH:mm:ss')
    })
    return {
      count,
      rows: list,
    };
  }

  async addItem(req: RepairAddItem & RepairsStatusDto, userId: number) {
    const result = await Repair.create({
      ...req,
      userId: userId,
    });
    return result;
  }

  async editItem(req: RepairEditItems) {
    const result = await Repair.update(
      {
        ...req,
      },
      {
        where: {
          id: req.id,
        },
      }
    );
    return result;
  }

  async byIdItem(req: RepairId) {
    const result = await Repair.findOne({
      where: {
        id: req.id,
      },
      attributes: { exclude: ['createdAt', 'updatedAt'] },
      include: [
        {
          model: House,
          attributes: ['pointName', 'building', 'room'],
        },
        {
          model: RepairItem,
          attributes: ['name'],
        },
        {
          model: User,
          attributes: ['name'],
        },
      ],
    });
    const newResult = new RepairUseDTO(result.get({ plain: true }))
    newResult['appointment'] = dayjs(newResult['appointment']).format('YYYY-MM-DD HH:mm:ss')
    return newResult;
  }
  async updateStatus(req: RepairId & RepairsStatusDto) {
    const result = await Repair.update(
      {
        status: req.status,
      },
      {
        where: {
          id: req.id,
        },
      }
    );
    return result;
  }

  async delItem(req: RepairId) {
    const result = await Repair.destroy({
      where: {
        id: req.id,
      },
    });
    return result;
  }
}
